// run: time -p $exec < input > output
#include <iostream>
#include <string>

bool const big[13] = {0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1};
int const w[] = {1, 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7};
std::string s;

// big month 1 3 5 7 8 10 12

bool leap(int x)
{
	if (x % 4) return false;
	if (!(x % 100) && (x % 400)) return false;
	return true;
}

bool ok19(int y1, int y2, int m1, int m2, int d1, int d2)
{
	int year = 1900 + y1 * 10 + y2;
	int mon = m1 * 10 + m2;
	int day = d1 * 10 + d2;
	if (!mon || !day || mon > 12 || day > 31) return false;
	if (mon == 2) {
		int lim = 28;
		if (leap(year)) lim++;
		if (day > lim) return false;
	} else if (!big[mon]) {
		if (day > 30) return false;
	}
	return true;
}

bool ok20(int y1, int y2, int m1, int m2, int d1, int d2)
{
	int year = 2000 + y1 * 10 + y2;
	int mon = m1 * 10 + m2;
	int day = d1 * 10 + d2;
	if (year > 2011) return false;
	if (!mon || !day || mon > 4 || day > 31) return false;
	if (mon == 2) {
		int lim = 28;
		if (leap(year)) lim++;
		if (day > lim) return false;
	} else if (mon == 4) {
		if (day > 2) return false;
	} else if (!big[mon]) {
		if (day > 30) return false;
	}
	return true;
}

int diff(std::string const & s1, std::string const & s2)
{
	int ret = 0;
	for (int i = 0; i < 18; i++)
		ret += s1[i] != s2[i];
	return ret;
}

std::string first(std::string const & s)
{
	int min = 1 << 30;
	std::string ans;
	for (int y1 = 0; y1 <= 9; y1++) {
		int s1 = (y1 != s[6] - '0');
		if (s1 > min) continue;
		for (int y2 = 0; y2 <= 9; y2++) {
			int s2 = s1 + (y2 != s[7] - '0');
			if (s2 > min) continue;
			for (int m1 = 0; m1 <= 9; m1++) {
				int s3 = s2 + (m1 != s[8] - '0');
				if (s3 > min) continue;
				for (int m2 = 0; m2 <= 9; m2++) {
					int s4 = s3 + (m2 != s[9] - '0');
					if (s4 > min) continue;
					for (int d1 = 0; d1 <= 9; d1++) {
						int s5 = s4 + (d1 != s[10] - '0');
						if (s5 > min) continue;
						for (int d2 = 0; d2 <= 9; d2++) {
							int s6 = s5 + (d2 != s[11] - '0');
							if (s6 > min) continue;
							std::string ts = s;
							ts[6] = y1 + '0';
							ts[7] = y2 + '0';
							ts[8] = m1 + '0';
							ts[9] = m2 + '0';
							ts[10] = d1 + '0';
							ts[11] = d2 + '0';
							if (ok20(y1, y2, m1, m2, d1, d2) || ok19(y1, y2, m1, m2, d1, d2)) {
								int t = diff(ts, s);
								if (t < min) {
									min = t;
									ans = ts;
								}
							}
						}
					}
				}
			}
		}
	}
	return ans;
}

bool check(std::string const & s)
{
	int sum = 0;
	for (int i = 1; i <= 17; i++)
		sum += w[i] * (s[17 - i] - '0');
	int t = (12 - (sum % 11)) % 11;
	if (t < 10 && t == (s[17] - '0')) return true;
	if (t == 10 && s[17] == 'X') return true;
	return false;
}

std::string second(std::string const & s)
{
	int min = 1 << 30;
	std::string ans;
	for (int y1 = 0; y1 <= 9; y1++) {
		int s1 = (y1 != s[8] - '0');
		if (s1 > min) continue;
		for (int y2 = 0; y2 <= 9; y2++) {
			int s2 = s1 + (y2 != s[9] - '0');
			if (s2 > min) continue;
			for (int m1 = 0; m1 <= 9; m1++) {
				int s3 = s2 + (m1 != s[10] - '0');
				if (s3 > min) continue;
				if (m1 > 1) continue;
				for (int m2 = 0; m2 <= 9; m2++) {
					int s4 = s3 + (m2 != s[11] - '0');
					if (s4 > min) continue;
					if (m1 * 10 + m2 > 12) continue;
					for (int d1 = 0; d1 <= 9; d1++) {
						int s5 = s4 + (d1 != s[12] - '0');
						if (s5 > min) continue;
						if (d1 > 3) continue;
						for (int d2 = 0; d2 <= 9; d2++) {
							int s6 = s5 + (d2 != s[13] - '0');
							if (s6 > min) continue;
							for (int sum = 0; sum <= 10; sum++) {
								std::string ts = s;
								ts[6] = '1';
								ts[7] = '9';
								ts[8] = y1 + '0';
								ts[9] = y2 + '0';
								ts[10] = m1 + '0';
								ts[11] = m2 + '0';
								ts[12] = d1 + '0';
								ts[13] = d2 + '0';
								ts[17] = sum + '0';
								if (sum == 10) ts[17] = 'X';
								if (check(ts) && ok19(y1, y2, m1, m2, d1, d2)) {
									int t = diff(ts, s);
									if (t < min) {
										min = t;
										ans = ts;
									}
								}
							}
						}
					}
				}
			}
		}
	}

	for (int y1 = 0; y1 <= 9; y1++) {
		int s1 = (y1 != s[8] - '0');
		if (s1 > min) continue;
		if (y1 > 1) continue;
		for (int y2 = 0; y2 <= 9; y2++) {
			int s2 = s1 + (y2 != s[9] - '0');
			if (s2 > min) continue;
			if (y1 * 10 + y2 > 11) continue;
			for (int m1 = 0; m1 <= 9; m1++) {
				int s3 = s2 + (m1 != s[10] - '0');
				if (s3 > min) continue;
				if (m1 > 1) continue;
				for (int m2 = 0; m2 <= 9; m2++) {
					int s4 = s3 + (m2 != s[11] - '0');
					if (s4 > min) continue;
					for (int d1 = 0; d1 <= 9; d1++) {
						int s5 = s4 + (d1 != s[12] - '0');
						if (s5 > min) continue;
						if (d1 > 3) continue;
						for (int d2 = 0; d2 <= 9; d2++) {
							int s6 = s5 + (d2 != s[13] - '0');
							if (s6 > min) continue;
							for (int sum = 0; sum <= 10; sum++) {
								std::string ts = s;
								ts[6] = '2';
								ts[7] = '0';
								ts[8] = y1 + '0';
								ts[9] = y2 + '0';
								ts[10] = m1 + '0';
								ts[11] = m2 + '0';
								ts[12] = d1 + '0';
								ts[13] = d2 + '0';
								ts[17] = sum + '0';
								if (sum == 10) ts[17] = 'X';
								if (check(ts) && ok20(y1, y2, m1, m2, d1, d2)) {
									int t = diff(ts, s);
									if (t < min) {
										min = t;
										ans = ts;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return ans;
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		std::cin >> s;
		if (s.size() == 15)
			std::cout << first(s) << '\n';
		else
			std::cout << second(s) << '\n';
	}
}

